﻿"use strict";



// Cordova is ready
//
function onDeviceReady() {
     
}

// 初始化動作
window.onload = function() {
    gCanvas.height = document.body.offsetHeight * 0.8;
    gCanvas.width  = document.body.offsetWidth;

    gTextWidth = gCanvas.width / 4;
}

// 檢查點擊的地方是否在合法繪圖範圍內
function inScope( x, y )
{
    return ( x > 0 && x < gCanvas.width && y > 0 && y < gCanvas.height );
}


// 滑鼠左鍵放開的事件
document.ontouchend = function( event )
{
    if ( gMouseState == MOUSE_DOWN )
    {
        var x = event.pageX + document.documentElement.scrollLeft;
        var y = event.pageY + document.documentElement.scrollTop;
        
        if ( inScope( x, y ) )
        {
            gMouseUpX = x;
            gMouseUpY = y;
            gMouseState = MOUSE_UP;
            mouseup( gMouseUpX, gMouseUpY );
        }     
    }
}

// 滑鼠左鍵放開的事件
document.onmouseup = function( event )
{
    if ( gMouseState == MOUSE_DOWN )
    {
        var x = event.pageX + document.documentElement.scrollLeft;
        var y = event.pageY + document.documentElement.scrollTop;
        
        if ( inScope( x, y ) )
        {
            gMouseUpX = x;
            gMouseUpY = y;
            gMouseState = MOUSE_UP;
            mouseup( gMouseUpX, gMouseUpY );
        }     
    }
}

// 滑鼠左鍵放開的事件
document.onmouseout = function( event )
{
    var x = event.pageX + document.documentElement.scrollLeft;
    var y = event.pageY + document.documentElement.scrollTop;
    //mouseup( x, y );
}

// 滑鼠左鍵按下的事件
document.ontouchstart = function( event )
{
    var x = event.pageX + document.documentElement.scrollLeft;
    var y = event.pageY + document.documentElement.scrollTop;
    
    if ( inScope( x, y ) )
    {
        gMouseState = MOUSE_DOWN;
        gMouseDownX = x;
        gMouseDownY = y;
    }
}

// 滑鼠左鍵按下的事件
document.onmousedown = function( event )
{
    var x = event.pageX + document.documentElement.scrollLeft;
    var y = event.pageY + document.documentElement.scrollTop;
    
    if ( inScope( x, y ) )
    {
        gMouseState = MOUSE_DOWN;
        gMouseDownX = x;
        gMouseDownY = y;
    }
}

// for ios, android .
document.ontouchmove = function( event )
{
    try
    {
        // 當滑鼠按下後才開始記錄軌跡
        if ( gMouseState == MOUSE_DOWN )
        {
            var x = event.pageX + document.documentElement.scrollLeft;
            var y = event.pageY + document.documentElement.scrollTop;

            if ( inScope( x, y ) )
            {
                gCutEnable = false;
                mousemove( x, y );
            }
        }
    }
    catch ( err )
    {
        printError( "發生錯誤: " + err.stack + "<br>" );
    }
}

// 滑鼠左鍵移動的事件
document.onmousemove = function( event )
{
    try
    {
        // 當滑鼠按下後才開始記錄軌跡
        if ( gMouseState == MOUSE_DOWN )
        {
            var x = event.pageX + document.documentElement.scrollLeft;
            var y = event.pageY + document.documentElement.scrollTop;

            if ( inScope( x, y ) )
            {
                gCutEnable = false;
                mousemove( x, y );
            }
        }
    }
    catch ( err )
    {
        printError( "發生錯誤: " + err.stack + "<br>" );
    }
}

// 滑鼠左鍵放開的事件
function mouseup( pageX, pageY )
{
    var phyX = pageX; // 取得x位置
    var phyY = pageY; // 取得y位置  

    //showAlert( pageX + "," + pageY );

    var buttonNo = getButtonNo( phyX, phyY );

    if ( gNowPage == PAGE_COLOR || gNowPage == PAGE_PEN )
    {
        var colorNo = buttonNo;

        if ( colorNo != NOT_FOUND ) {
            gPenColor = gColors[colorNo];
            printDebug( gColors[colorNo] );
            //printDebug( gPenHistory );
            gPenHistory += getGlobalValueHistory(); // 更新設置
            //showAlert( "X" );
            //printDebug( gPenHistory );
        }

        showStoredDrawing( NOWDO );
        showDrawingPage();
    }
    else if ( buttonNo != NOT_FOUND )
    {
        if ( enterPageByButton( buttonNo ) )
        {

        }
        else
        {
            
        }
    }
    else if ( gNowPage == PAGE_DRAWING )
    {
        if ( !gCutEnable )
        {
            gCutEnable = true; // 斷掉連線
            gPreviousPhyX = INIT_POS; // 上一次畫筆的位置x
            gPreviousPhyY = INIT_POS; // 上一次畫筆的位置y
            
            gPenHistory += TOUCH_GAP; // 總結此次下筆紀錄 
            
            storeNowDrawing(); // 存入目前畫面
        }
        else if ( !penIsLock() )
        {
            draw( phyX, phyY );

            gPenHistory += TOUCH_GAP; // 總結此次下筆紀錄 
            
            //storeNowDrawing(); // 存入目前畫面
        }
        
    }
}

function mousedown()
{
    

    
}

// 滑鼠移動的事件
function mousemove( pageX, pageY )
{
    var phyX = pageX; // 取得x位置
    var phyY = pageY; // 取得y位置

    

    if ( !penIsLock() && gPreviousPhyX != INIT_POS )
    {
        // 將現在的點和之前的點連起來
        draw( phyX, phyY );
    }
    
    gPreviousPhyX = phyX;
    gPreviousPhyY = phyY;
    gPreviousTime = gNowTime;
}
